<template>
  <div class="mallOrder-list">
    <search-list
      ref="mallOrderList"
      :api-url="$url.MallOrderList"
      :option="searchOpt"
      :itemPerRow="6"
      :beforeQuery="beforeQuery"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="订单信息">
        <template slot-scope="scope">
          <p>编号：{{ scope.row.orderId }}</p>
          <p>下单时间：{{ scope.row.createdAt }}</p>
          <p>类型：{{ $selectFormat['mallOrderTypeSl'](scope.row.kind).label }}</p>
          <p>来源：{{ $selectFormat['mallOrderPlatformSl'](scope.row.platform).label }}</p>
        </template>
      </el-table-column>
      <el-table-column label="商品信息">
        <template slot-scope="scope">
          <div class="goods-img">
            <div class="goods-left">
              <img v-if="scope.row.cover" :src="scope.row.cover" alt="" class="goods" />
            </div>
            <div class="goods-right">
              <p>{{ scope.row.title }}</p>
              <p class="diff-color" v-if="scope.row.skuPath">
                <span v-for="(item, index) in scope.row.skuPath.split('^V^')" :key="index">
                  {{ index % 2 ? item : '' }}
                </span>
              </p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="购买信息">
        <template slot-scope="scope">
          <p>购买用户ID：{{ scope.row.uuid }}</p>
          <p>购买用户昵称：{{ scope.row.nickname }}</p>
          <p>联系方式：{{ scope.row.phone }}</p>
          <p>支付方式：{{ $selectFormat['mallOrderPaySl'](scope.row.payType).label }}</p>
        </template>
      </el-table-column>
      <el-table-column label="支付信息">
        <template slot-scope="scope">
          <p>商品总价：￥{{ scope.row.totalPrice }}</p>
          <p>实付款：￥{{ scope.row.payPrice }}</p>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" width="80px">
        <template slot-scope="scope">
          <p>{{ $selectFormat['mallOrderStatusSl'](scope.row.orderStatus).label }}</p>
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
          <p v-if="scope.row.remark">备注：{{ scope.row.remark }}</p>
          <p v-if="scope.row.remarkTime">（{{ scope.row.remarkTime }}编辑）</p>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="orderDetail.includes(scope.row.orderStatus)"
            @click="goOrderDetail(scope.row)"
          >
            订单详情
          </el-button>
          <el-button
            type="text"
            v-if="orderClosed.includes(scope.row.orderStatus)"
            @click="cloesdOrder(scope.row)"
          >
            订单关闭
          </el-button>
          <el-button
            type="text"
            v-if="orderNote.includes(scope.row.orderStatus)"
            @click="NoteOrder(scope.row)"
          >
            订单备注
          </el-button>
          <el-button
            type="text"
            v-if="deliverGoods.includes(scope.row.orderStatus)"
            @click="deliver(scope.row)"
          >
            发货配置
          </el-button>
          <el-button
            type="text"
            v-if="afterSale.includes(scope.row.orderStatus)"
            @click="afterBuy(scope.row)"
          >
            售后处理
          </el-button>
          <el-button
            type="text"
            v-if="orderLogistics.includes(scope.row.orderStatus)"
            @click="deliverInfo(scope.row)"
          >
            订单物流
          </el-button>
          <el-button
            type="text"
            v-if="delOrder.includes(scope.row.orderStatus)"
            @click="orderDel(scope.row)"
          >
            删除订单
          </el-button>
        </template>
      </el-table-column>

      <div slot="btn-after" class="btn-after"></div>

      <div slot="tab-before" class="tab-before">
        <el-radio-group v-model="searchStatus" :border="false" size="small">
          <el-radio-button label="-1">全部</el-radio-button>
          <el-radio-button label="0">待付款</el-radio-button>
          <el-radio-button label="1">待发货</el-radio-button>
          <el-radio-button label="2">待收货</el-radio-button>
          <el-radio-button label="3">已完成</el-radio-button>
          <el-radio-button label="4">已关闭</el-radio-button>
        </el-radio-group>
      </div>
    </search-list>

    <!-- dialog -->
    <el-dialog
      :title="dailogTitle"
      :visible="dialogVisible"
      width="60%"
      @close="dialogCancel"
      :append-to-body="true"
      :destroy-on-close="true"
    >
      <component
        :is="componentId"
        :info="info"
        @dialogCancel="dialogCancel"
        @dialogSubmit="dialogSubmit"
      ></component>
    </el-dialog>
  </div>
</template>

<script>
  import Deliver from './components/Deliver';
  import Note from './components/Note';
  import AfterSale from './components/AfterSale';
  import Logistics from './components/Logistics';
  import { stringify } from 'qs';
  export default {
    data() {
      return {
        // 组件传递下去的信息
        info: '',
        // 绑定的组件是哪个
        componentId: '',
        // dialog
        dialogVisible: false,
        // dialog标题
        dailogTitle: '',
        // 多选
        selectedData: [],
        // 筛选不同的状态
        searchStatus: '-1',
        // 不同的订单状态展示不同的编辑按钮
        orderDetail: [0, 1, 2, 4, 5, 6, 7, 8, 9],
        orderClosed: [0],
        orderNote: [0, 1, 2, 4, 5, 6, 7, 8, 9],
        deliverGoods: [1, 2],
        afterSale: [1, 2, 9],
        orderLogistics: [2, 9],
        delOrder: [ 7, 8],
        // 创造者筛选
        searchOpt: [
          {
            type: this.SEARCH_COMP_ENUM.SEARCH_INPUT,
            name: 'orderId',
            label: '订单编号',
          },
          {
            type: this.SEARCH_COMP_ENUM.SEARCH_INPUT,
            name: 'uuid',
            label: '购买用户ID',
          },
          {
            type: 'mallOrderTypeSl',
            name: 'kind',
            label: '订单类型',
          },
          {
            type: 'mallOrderPaySl',
            name: 'payType',
            label: '支付方式',
          },
          {
            type: 'mallOrderPlatformSl',
            name: 'platform',
            label: '订单来源',
          },
          {
            type: 'el-date-picker',
            name: 'updatedAt',
            $attrs: {
              type: 'daterange',
              format: 'yyyy-MM-dd',
              'value-format': 'yyyy-MM-dd',
              'start-placeholder': '开始日期',
              'end-placeholder': '结束日期',
            },
          },
        ],
      };
    },
    watch: {
      searchStatus: function(val) {
        this.$refs.mallOrderList.s_fetchData();
      },
    },
    methods: {
      // dialog取消
      dialogCancel() {
        this.dialogVisible = false;
        this.dailogTitle = '';
        this.componentId = '';
        this.info = null;
      },
      // dialog提交
      dialogSubmit() {
        // 取消
        this.dialogCancel();
        this.$refs.mallOrderList.getList();
      },
      // 删除订单
      orderDel(row) {
        this.$confirm('确认删除这条数据吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$service.MallOrderDelete({ id: row.id }).then((res) => {
              this.$message.success('删除成功！');
              this.$refs.mallOrderList.getList();
            });
          })
          .catch(() => {});
      },

      // 订单物流
      deliverInfo(row) {
        this.dialogVisible = true;
        this.dailogTitle = '';
        this.componentId = Logistics;
        this.info = JSON.parse(JSON.stringify(row));
      },

      // 售后处理
      afterBuy(row) {
        this.dialogVisible = true;
        this.dailogTitle = '选择售后处理方式';
        this.componentId = AfterSale;
        this.info = row.id;
      },

      // 发货配置
      deliver(row) {
        // 这列得先得到收获信息然后一起传下去
        this.$service.MallOrderReceiver({ id: row.id }).then((res) => {
          this.dialogVisible = true;
          this.dailogTitle = '';
          this.componentId = Deliver;
          let _ = res ? res : {};
          _.id = row.id;
          _.companyCode = _.companyCode || '';
          _.logisticNo = _.logisticNo || '';
          this.info = JSON.parse(JSON.stringify(_));
        });
      },

      // 订单备注
      NoteOrder(row) {
        this.dialogVisible = true;
        this.dailogTitle = '编辑备注';
        this.componentId = Note;
        this.info = JSON.parse(JSON.stringify(row));
      },

      // 订单关闭
      cloesdOrder(row) {
        this.$confirm('确定要关闭所选的订单吗？', '请确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            // 7对应后台关闭
            this.$service.MallOrderClose({ id: row.id, orderStatus: 7 }).then((res) => {
              this.$message.success('关闭成功！');
              this.$refs.mallOrderList.getList();
            });
          })
          .catch(() => {});
      },

      // 订单详情
      goOrderDetail(row) {
        const { id } = row;
        this.$tabs.open({ name: 'MallOrderDetail', params: { id: id } });
      },

      // 多选
      selectionChange(val) {
        this.selectedData = [...val];
      },

      // 筛选之前
      beforeQuery({ searchInfo }) {
        if (this.searchStatus !== -1) {
          searchInfo.orderStatus = this.searchStatus;
        }
        if (searchInfo.updatedAt === null) {
          searchInfo.startDate = '';
          searchInfo.endDate = '';
        } else {
          searchInfo.startDate = searchInfo.updatedAt[0];
          searchInfo.endDate = searchInfo.updatedAt[1];
        }
      },
    },
  };
</script>
<style lang="less" scoped>
  // 图片
  .goods {
    width: 60px;
    height: 60px;
    max-width: none;
    border-radius: 0px;
  }
  .tab-before {
    margin-top: -10px;
  }
  // 不同的颜色
  .diff-color {
    color: #aaa;
  }

  // 单独样式
  .mallOrder-list {
    // 图片展示
    .goods-img {
      display: flex;
      .goods-right {
        margin-left: 10px;
      }
    }
  }
  /deep/ .el-dialog {
    h3 {
      margin: 20px 0px;
      &:first-child {
        margin-top: 0px;
      }
    }
  }
</style>
